package com.hefu.virtuallife.ui.purchase;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.dz.utlis.AndroidUtils;
import com.dz.utlis.ClassTools;
import com.dz.utlis.ScreenUtils;
import com.hefu.virtuallife.R;
import com.hefu.virtuallife.baseui.BaseActivity;
import com.hefu.virtuallife.recyclerViewdivider.HDividerItemDecoration;
import com.hefu.virtuallife.ui.purchase.adapter.HomePurchaseAdapter;
import com.hefu.virtuallife.ui.purchase.bean.HomePurchaseBean;
import com.hefu.virtuallife.utils.UiCompat;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * creat_user: zhengzaihong
 * email:1096877329@qq.com
 * creat_date: 2018/8/13
 * creat_time: 18:29
 * describe: 家庭采购
 **/
public class HomePurchaseActivity extends BaseActivity implements OnRefreshLoadMoreListener {
    @BindView(R.id.iv_back)
    ImageButton ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageButton ivRight;

    Banner banner;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private HomePurchaseAdapter homePurchaseAdapter;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_home_purchase;
    }


    @Override
    protected void initWidget() {
        super.initWidget();

        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setVisibility(View.VISIBLE);
        ivRight.setVisibility(View.VISIBLE);
        tvTitle.setText("AI家庭采购");
        ivBack.setOnClickListener(v -> finish());
        ivRight.setImageResource(R.drawable.ticket_personal);
    }

    @Override
    protected void initData() {
        super.initData();

        View topview = AndroidUtils.getView(mContext, R.layout.headview_home_banner);
        banner = topview.findViewById(R.id.banner);


        ivRight.setOnClickListener(view -> ClassTools.toAct(mContext, PersionInfoActivity.class));


        List<Integer> listS = new ArrayList<>();
        listS.add(R.mipmap.banner1);
        listS.add(R.mipmap.banner2);
        listS.add(R.mipmap.banner3);

        banner.setImages(listS);
        banner.setImageLoader(new GlideImageLoader());
        banner.start();
        banner.setOnBannerListener(position -> {
        });


        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.autoRefresh();
        refreshLayout.setEnableLoadMore(false);

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        HDividerItemDecoration dividerItemDecoration = new HDividerItemDecoration();
        dividerItemDecoration.setSize((int) ScreenUtils.dip2px(mContext, 10f));
        dividerItemDecoration.setColor(UiCompat.getColor(mContext.getResources(), R.color.line));
        recyclerView.addItemDecoration(dividerItemDecoration);
        homePurchaseAdapter = new HomePurchaseAdapter();
        recyclerView.setAdapter(homePurchaseAdapter);

        homePurchaseAdapter.addHeaderView(topview);


    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {

    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {


        List<HomePurchaseBean> listdata = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            HomePurchaseBean bean = new HomePurchaseBean();
            listdata.add(bean);
        }

        homePurchaseAdapter.replaceData(listdata);

        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();

    }

    public static class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object banner, ImageView imageView) {
//            Glide.with(context).load(banner.toString()).into(imageView);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setBackgroundResource(Integer.parseInt(banner.toString()));
        }

        @Override
        public ImageView createImageView(Context context) {
            return null;
        }
    }
}
